package com.laoguo.comfig;

import com.laoguo.properties.RabbitmqProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@EnableConfigurationProperties(value = RabbitmqProperties.class)
@Configuration
public class RabbitmqConfig {

    @Autowired
    private RabbitmqProperties properties;

    @Bean
    public ConnectionFactory connectionFactory(){
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(properties.getAddresses(),properties.getPort());
        cachingConnectionFactory.setUsername(properties.getUsername());
        cachingConnectionFactory.setPassword(properties.getPassword());
        cachingConnectionFactory.setVirtualHost(properties.getVirtualHost());
        cachingConnectionFactory.setPublisherConfirms(true);
        return cachingConnectionFactory;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate(){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }

    @Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }

    @Bean
    public Queue guojunjieQueue(){
        return new Queue("guojunjie",true);
    }
    @Bean
    public TopicExchange guojunjieExchange(){
        return new TopicExchange("topicExchange1",true,false);
    }

    @Bean
    public Binding bindGuoJunJieExchange(){
        return BindingBuilder.bind(guojunjieQueue()).to(guojunjieExchange()).with("topic.#");
    }

    @Bean
    public Queue delayQueue(){
        return new Queue("delay_queue",true);
    }


    /**
     * 死信交换机要注意，如果rabbitmq没有安装插件的话，会报错的。
     * 应该先安装 rabbitmq_delayed_message_exchange-20171201-3.7.x.ez 插件，然后启动插件，重启rabbitmq
     *
     */
    @Bean
    public CustomExchange delayExchange(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange("delay_exchange","x-delayed-message",true,false,args);
    }



    @Bean
    public Binding bindDelayExchange(){
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with("delay_message").noargs();
    }


}
